<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Insure extends Base
{
    protected $id = 'car_insure_id';
    protected $table = 'car_insure';
    protected $join = ['car c','c.car_id = m.car_id'];
    protected $field = 'm.*,c.number';

    //下载excel表格
    public function outexcel() {

        $P = input('param.');
        $where = [];

        $P['car_id'] && $where['car_id'] = ['=',$P['car_id']];
        $P['insurance_company'] && $where['insurance_company'] = ['LIKE',"%{$P['insurance_company']}%"];
        $P['policy_type'] && $where['policy_type'] = ['=',$P['policy_type']];
        $P['start_date'] && $where['start_date'] = ['>=',$P['start_date']];
        $P['end_date'] && $where['end_date'] = ['<=',$P['end_date']];

        $car_insure_return = Db::name('car_insure')->where($where)->whereNull('delete_time')->field('car_id,policy_number,policy_type,policy_charge,insurance_company,policy_holder,linkman,phone,start_date,end_date,ps')->order('start_date desc')->select();
        $car_return = Db::name('car')->where('')->field('number,car_id')->select();

        if($car_insure_return) {
            echo "<table border='1'>";
            //标题
            $title = array("车牌号","保单号","保险种类","购买金额","保险公司","投保人","保险公司联系人","保险公司联系电话","投保时间","到期时间","备注说明");
            //文件名
            $filename = '年检管理导出报表';
            header("Content-type:application/octet-stream");
            header("Accept-Ranges:bytes");
            header("Content-type:application/vnd.ms-excel");
            header("Content-Disposition:attachment;filename=".$filename.".xls");
            header("Pragma: no-cache");
            header("Expires: 0");
            if (!empty($title)) {
                foreach ($title as $k => $v) {
                    $title[$k] = iconv("UTF-8", "GB2312", "<td>" . $title[$k] . "</td>");
                    echo $title[$k];
                }
                echo "</tr>";
            }
            if (!empty($car_insure_return)) {
                foreach ($car_insure_return as $key => $val) {

                    if ($car_insure_return[$key]['policy_type']=='1'){
                        $car_insure_return[$key]['policy_type'] = "交通强制险";
                    }
                    else{
                        $car_insure_return[$key]['policy_type'] = "商业险";
                    }

                    foreach ($car_return as $key2 => $val2){
                        if($car_return[$key2]['car_id'] == $car_insure_return[$key]['car_id']){
                            $car_insure_return[$key]['car_id']= $car_return[$key2]['number'];
                        }
                    }

                    foreach ($val as $ck => $cv) {
                        $car_insure_return[$key][$ck] = iconv("UTF-8", "GB2312", "<td style=\"vnd.ms-excel.numberformat:@\">" . $car_insure_return[$key][$ck] . "</td>");
                        echo $car_insure_return[$key][$ck];
                    }
                    echo "</tr>";
                }
            }
        }
        else {
            $this->error('无数据');
        }
    }
}
